package com.cn.xiaonuo.tcrfinance.sysmgmt.controller;

import com.cn.xiaonuo.core.pojo.login.SysLoginUser;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.SysLoginHpcInfo;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.SysUserHpcEntity;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysLoginHpcParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysCaptchaHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysUserHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysUserTokenHpcService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 登录相关
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
public class SysLoginHpcController {
    @Autowired
    private SysUserHpcService sysUserService;
    @Autowired
    private SysUserTokenHpcService sysUserTokenService;
    @Autowired
    private SysCaptchaHpcService sysCaptchaService;

    /**
     * 验证码
     */
    @GetMapping("/api/common/captcha")
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    public void captcha(HttpServletResponse response, String uuid) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //获取图片验证码
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     * 登录
     */
    @PostMapping("/api/common/sys/login")
    @ApiOperation(value = "登录", notes = "登录")
    public ResponseData login(@RequestBody SysLoginHpcParam sysLoginParam) throws IOException {
        SuccessResponseData responseData = new SuccessResponseData();
        boolean captcha = sysCaptchaService.validate(sysLoginParam.getUuid(), sysLoginParam.getCaptcha());
        if (!captcha) {
            responseData.setMessage("验证码不正确");
            return responseData;
        }
        //用户信息
        SysUserHpcEntity user = sysUserService.queryByUserName(sysLoginParam.getUsername());

        //账号不存在、密码错误
        if (user == null || !user.getPassword().equals(new Sha256Hash(sysLoginParam.getPassword(), user.getSalt()).toHex())) {
            responseData.setMessage("账号或密码不正确");
            return responseData;
        }

        //账号锁定
        if (user.getStatus() == 0) {
            responseData.setMessage("账号已被锁定,请联系管理员");
            return responseData;
        }

        //生成token，并保存到数据库
        SysLoginUser slLoginUser = new SysLoginUser();//系统共用用户类
        slLoginUser.setId(user.getUserId());//设置用户id
        slLoginUser.setUsername(user.getUsername());//设置用户名
        slLoginUser.setPassword(user.getPassword());//设置密码
        SysLoginHpcInfo sysLoginInfo = sysUserTokenService.createToken(slLoginUser);
        return new SuccessResponseData(sysLoginInfo);
    }


    /**
     * 退出
     */
    @ApiOperation(value = "退出", notes = "退出")
    @PostMapping("/api/sys/logout")
    public ResponseData logout(@RequestBody SysLoginHpcParam sysLoginParam) {
        sysUserTokenService.logout(sysLoginParam.getUuid());
        return new SuccessResponseData();
    }
}
